package contacts.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.border.BevelBorder;

import contacts.controller.ContactsControllerListener;
import contacts.po.Student;
import contacts.service.ContactsService;

public class ContactsFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private ContactsControllerListener controllerListener;
	private JTextField searchtext;
	private JPanel treeWin;

	/**
	 * 主面板
	 */
	public ContactsFrame() {
		this.controllerListener = new ContactsControllerListener();
		this.treeWin = new TreeWin(ContactsService.findAllStudent());
		this.controllerListener.setContactsFrame(this);
		this.controllerListener.setTreeWin(treeWin);
		this.getNorthTool().setPreferredSize(new Dimension(200, 200));
		this.add(this.getNorthTool(), BorderLayout.NORTH);
		this.add(treeWin, BorderLayout.CENTER);
		this.setVisible(true);
		this.setSize(1260, 726);
		this.setResizable(false);
		this.setTitle("通讯录");
		this.setLocationRelativeTo(null);
		this.validate();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.validate();
	}

	/**
	 * 增、删、改工具栏
	 */
	private JPanel getStudentModifyToolBar() {
		JPanel toolBar = new JPanel();
		toolBar.setBorder(new BevelBorder(BevelBorder.RAISED));
		toolBar.setAlignmentX(RIGHT_ALIGNMENT);

		JButton addStudentButton = new JButton("添加");
		JButton deleteStudentButton = new JButton("删除");
		JButton updateStudentButton = new JButton("修改");

		ImageIcon updateIcon = new ImageIcon(ContactsFrame.class.getResource("/update.png"));
		ImageIcon deleteIcon = new ImageIcon(ContactsFrame.class.getResource("/delete.png"));
		ImageIcon addIcon = new ImageIcon(ContactsFrame.class.getResource("/add.jpg"));
		
		addStudentButton.setIcon(addIcon);
		deleteStudentButton.setIcon(deleteIcon);
		updateStudentButton.setIcon(updateIcon);
		
		addStudentButton.addActionListener(controllerListener);
		deleteStudentButton.addActionListener(controllerListener);
		updateStudentButton.addActionListener(controllerListener);

		addStudentButton.setActionCommand("addStudentDialog");
		deleteStudentButton.setActionCommand("deleteStudentDialog");
		updateStudentButton.setActionCommand("updateStudentDialog");

		toolBar.add(addStudentButton);
		toolBar.add(new JToolBar.Separator());
		toolBar.add(deleteStudentButton);
		toolBar.add(new JToolBar.Separator());
		toolBar.add(updateStudentButton);

		return toolBar;
	}

	/**
	 * 搜索工具栏
	 */
	private JPanel getFindTool() {
		JPanel findPanel = new JPanel();
		JLabel label = new JLabel("输入学生学号查询");
		searchtext = new JTextField(15);
		JButton findButton = new JButton("搜索");
		JButton refreshStudentButton = new JButton("查询所有学生");	
		ImageIcon selectIcon = new ImageIcon(ContactsFrame.class.getResource("/select1.png"));	
		findButton.setIcon(selectIcon);
		refreshStudentButton.setIcon(selectIcon);	
		refreshStudentButton.addActionListener(controllerListener);
		refreshStudentButton.setActionCommand("refreshStudentJTable");
		findButton.addActionListener(controllerListener);
		findButton.setActionCommand("searchPost");
		findPanel.setBorder(new BevelBorder(BevelBorder.RAISED));
		findPanel.add(label);
		findPanel.add(searchtext);
		findPanel.add(findButton);
		findPanel.add(refreshStudentButton);
		return findPanel;
	}

	/**
	 * 获得查询框
	 */
	public JTextField getSearchtext() {
		return searchtext;
	}

	/**
	 * 增删改查面板
	 */
	private JSplitPane getNorthTool() {
		JSplitPane function = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, getStudentModifyToolBar(), getFindTool());
		return function;
	}

	/**
	 * 获得controllerListener
	 */
	public ContactsControllerListener getControllerListener() {
		return controllerListener;
	}

	/**
	 * 重新加载树组件表格
	 */
	public void refresh(List<Student> findAllStudent) {
		this.remove(treeWin);
		treeWin = new TreeWin(findAllStudent);
		this.add(treeWin, BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功");
	}

}
